﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class FareOverview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("FA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                    return;
                }

                if (Session["ORI"] != null)
                    txtOriAirport.Text = Session["ORI"].ToString();
                if (Session["DES"] != null)
                    txtDesAirport.Text = Session["DES"].ToString();
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            txtOriAirport.Text = txtOriAirport.Text.Trim().ToUpper();
            txtDesAirport.Text = txtDesAirport.Text.Trim().ToUpper();
            Session["ORI"] = txtOriAirport.Text;
            Session["DES"] = txtDesAirport.Text;

            StringBuilder strout = new StringBuilder();
            string last_fare="\t";
            int height;
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            DataTable dtOut = new DataTable();
            cmd.CommandText = "bll_fare_get_overview";
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@ori_airport", txtOriAirport.Text);
            para = cmd.Parameters.AddWithValue("@des_airport", txtDesAirport.Text);
            para = cmd.Parameters.AddWithValue("@seg_total", lstSegTotal.SelectedValue);
            para = cmd.Parameters.AddWithValue("@is_public", chkQpublic.Checked);
            para = cmd.Parameters.AddWithValue("@is_open", chkQopen.Checked);
            try
            {
                strout.Append("<table border=0><tr class='my_table_header' align='center'><th style='min-width:250px;' align='left'>Product-Code</th><th style='min-width:50px;'>Cabin</th><th style='min-width:50px;'>Class</th><th style='min-width:80px;'>Min-Seats</th><th style='min-width:70px;'>Currency</th><th colspan=500>Flight-Price</th></tr>");
                TinyDAL.Exec(ref cmd, ref dtOut);
                foreach(DataRow row in dtOut.Rows)
                {
                    if(last_fare!=row["code"].ToString())
                    {
                        if(last_fare != "\t")
                            strout.Append("</tr>");

                        last_fare = row["code"].ToString();
                        strout.Append("<tr align='center'><td align='left'><a href='FareStairs.aspx?fare_code=" + last_fare.TrimEnd()+"&seg_sn="+row["seg_sn"].ToString()+"'>"+ last_fare.TrimEnd()+"</a></td><td>"
                            +row["cabin"].ToString()+"</td><td>"+row["class"].ToString()+"</td><td>"+ row["seats_min"].ToString()+"</td><td>"+row["currency"].ToString()+"</td>");
                    }
                    strout.Append("<td valign='bottom' align='center' style='min-width:70px'>"+Convert.ToDecimal(row["adt_price"]).ToString("F0")+"<br/>");
                    if (Convert.ToDecimal(row["adt_price"]) < 0)
                        strout.Append("<img src='images/bar_red.bmp'");
                    else
                    {
                        if (Convert.ToBoolean(row["approved"]) && row["action"].ToString() == "-")
                            strout.Append("<img src='images/bar_green.bmp'");
                        else if (Convert.ToBoolean(row["approved"]) && row["action"].ToString() != "-")
                            strout.Append("<img src='images/bar_blue.bmp'");
                        else
                            strout.Append("<img src='images/bar_yellow.bmp'");
                    }
                    height = Convert.ToInt16(row["adt_price"]) / 20;
                    if (height < 10)
                        height = 10;
                    strout.Append(" width=10 height=" + height.ToString() + "><br/> " + Convert.ToInt16(row["fltdate_diff"]).ToString("+#;-#;0") + "</td>");
                }
                if (last_fare != "\t")
                    strout.Append("</tr>");
                
                strout.Append("</table>");
                litTable.Text = strout.ToString();
            }
            catch(Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}